package com.resou.reader.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.resou.reader.R;
import com.resou.reader.reader.utils.SpUtils;

/**
 * Created by Garrett on 2018/9/9.
 * contact me krouky@outlook.com
 */
public class Loading extends Fragment {

    private static Loading sLoading;
    private TextView loadingTV;
    private boolean isShow;
    private int view_layout = 0;
    LinearLayout loading_layout;
    ImageView ivLoading_gif;

    public Loading() {
    }

    @SuppressLint("ValidFragment")
    public Loading(int view_layout) {
        this.view_layout = view_layout;
    }

    public static Loading getInstance() {
        if (sLoading == null) {
            synchronized (Loading.class) {
                if (sLoading == null) {
                    sLoading = new Loading();
                }
            }
        }
        return sLoading;
    }

    public static Loading getInstance(int view_layout) {
        if (sLoading == null) {
            synchronized (Loading.class) {
                if (sLoading == null) {
                    sLoading = new Loading(view_layout);
                }
            }
        }
        return sLoading;
    }

    private CharSequence loadingText = "";

    public void setLoadingText(CharSequence text) {
        loadingText = text;
        if (loadingTV != null) {
            loadingTV.setText(text);
        }
    }

    /**
     * 设置显示视图
     *
     * @param view
     */
    public void setView(int view) {
        this.view_layout = view;
    }

    FragmentManager fm;

    public void show(FragmentManager fm) {
        try {
            if (!isShow() && isAdded() && !isRemoving() && !isVisible()) {
                if (this.fm != null) {
                    this.fm.beginTransaction().remove(this).commitAllowingStateLoss();
                } else {
                    return;
                }
            }

            this.fm = fm;
            isShow = true;
            if (fm != null && fm.beginTransaction() != null) {
                fm.beginTransaction().add(android.R.id.content, this).detach(this).attach(this).commitAllowingStateLoss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismiss() {
        loadingText = "正在加载...";
        try {
            if (fm != null) {
                fm.beginTransaction().remove(this).commitAllowingStateLoss();
            }
            fm = null;
            isShow = false;
        } catch (Exception e) {
            Log.d("guolong", "111 error");
            e.printStackTrace();
        }
    }

    public boolean isShow() {
        return isShow;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = null;
        if (view_layout == 0) {
            view = inflater.inflate(R.layout.loading_layout, container, false);
            loading_layout = view.findViewById(R.id.loading_layout);
            loading_layout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
        } else if (view_layout == 2) {
            view = inflater.inflate(R.layout.layout_loading_reader, container, false);
            loading_layout = view.findViewById(R.id.loading_layout);
            if (SpUtils.getTheme(getActivity())) {
                loading_layout.setBackgroundColor(getResources().getColor(R.color.color_282828));
            } else {
                loading_layout.setBackgroundColor(getResources().getColor(R.color.white));
            }
            loading_layout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
            ivLoading_gif = view.findViewById(R.id.ivLoading_gif);
            RequestOptions options = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .format(DecodeFormat.PREFER_RGB_565);
            Glide.with(view).asGif().apply(options).load(R.drawable.loading_reader).apply(options).into(ivLoading_gif);
            view_layout = 0;
        } else {
            view = inflater.inflate(view_layout, container, false);
            view_layout = 0;
        }
        loadingTV = view.findViewById(R.id.loading_text);
        Log.i("进度条", "onCreateView:view_layout" + view_layout);
//        loadingTV.setText(loadingText);
        return view;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        Animation anim = null;
        if (enter) {
            try {
                anim = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_loading_enter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                anim = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_loading_exit);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return anim;
    }


}
